//
//  YCAlertView.m
//  lianganyuchu
//
//  Created by iMac on 17/3/24.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCAlertView.h"
#define BtnWidth 200
#define BtnHeight 40

@interface YCAlertView ()
{
    UILabel *m_labTitle;
    UIButton *m_btnHide;
}
@end
@implementation YCAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addMainViewWith:frame];
    }
    return self;
}
-(void)addMainViewWith:(CGRect)frame
{
    UIView *back=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    [back setBackgroundColor:[UIColor blackColor]];
    [back setAlpha:0.1];
    [self addSubview:back];
    
    UIView *centerView=[[UIView alloc]init];
    [centerView setBackgroundColor:WHITE_COLOR];
    centerView.layer.cornerRadius=4.0f;
    [self addSubview:centerView];
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(back).offset(200);
        make.centerX.equalTo(back.centerX);
        make.width.mas_equalTo(280);
        make.height.mas_equalTo(170);
    }];

    CGFloat left=(280-200)/2;
    m_labTitle=[[UILabel alloc]initWithFrame:CGRectMake(left, 40, 200, 20)];
    [m_labTitle setTextColor:TEXT_MIDDLE];
    [m_labTitle setFont:[UIFont LightFontOfSize:20]];
    [m_labTitle setTextAlignment:NSTextAlignmentCenter];
    [centerView addSubview:m_labTitle];
    
    m_btnHide=[[UIButton alloc]initWithFrame:CGRectMake(left, 170-BtnHeight-30, BtnWidth, BtnHeight)];
    [m_btnHide setTitle:@"我知道了" forState:UIControlStateNormal];
    [m_btnHide.titleLabel setFont:[UIFont LightFontOfSize:16]];
    m_btnHide.layer.cornerRadius=2.0f;
    [m_btnHide setBackgroundColor:MAIN_COLOR];
    [m_btnHide addTarget:self action:@selector(btnHideClicked:) forControlEvents:UIControlEventTouchUpInside];
    [centerView addSubview:m_btnHide];
    
}
-(void)btnHideClicked:(UIButton *)button
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hide];
    });
}
-(void)showAlertWith:(NSString *)title
{
    [m_labTitle setText:title];
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self show];
}
-(void)show
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    self.alpha = 0;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    }];
}
-(void)hide
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

@end
